#include <stdio.h>
#include <string>
#include "SdpFactory.h"

using namespace STL;

std::string readStringFromFile(std::string fullpath, long offset = 0, long len = 0, long *totle = nullptr)
{
    std::string ret = "";
    if (!fullpath.empty())
    {
        unsigned char *buffer = nullptr;
        long size = 0;
        size_t readsize = 0;

        do
        {
            FILE *fp = fopen(fullpath.c_str(), "rb");
            if (!fp)
                break;
            fseek(fp, 0, SEEK_END);
            size = ftell(fp);
            if (totle)
                *totle = size;
            if (0 == len)
                len = size - offset;
            if (size - offset < len)
                len = size - offset;
            if (0 == len)
                return ret;
            fseek(fp, offset, SEEK_SET);
            buffer = (unsigned char *)malloc(sizeof(unsigned char) * len);
            if (nullptr == buffer)
                return ret;
            readsize = fread(buffer, sizeof(unsigned char), len, fp);
            if (0 == readsize)
                printf("Get data from file %s failed", fullpath.c_str());
            fclose(fp);
        } while (0);
        ret = std::string((const char *)buffer, readsize);
        free(buffer);
    }
    return ret;
}

int main(int argn, char **argv)
{
    std::string sdp = readStringFromFile("./resources/a.sdp");
    printf("==============================================\n");
    printf("Parse\n");
    printf("----------------------------------------------\n");
    printf("%s\n", sdp.c_str());
    printf("----------------------------------------------\n");

    GBSdpOptions opt = GBSdpFactory().parseSdp(sdp);
    std::string newSdp = GBSdpFactory().genSdp(opt);
    printf("==============================================\n");
    printf("Generate\n");
    printf("----------------------------------------------\n");
    printf("%s\n", newSdp.c_str());
    printf("----------------------------------------------\n");
    
    printf("Hello World!\n");
    return 0;
}